package com.ksyun.campus.client;

import com.ksyun.campus.client.domain.ClusterInfo;
import com.ksyun.campus.client.domain.StatInfo;
import com.ksyun.campus.client.util.ZkUtil;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.jboss.netty.handler.codec.http.HttpMethod;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 自动化测试类
 */
public class EFileSystem extends FileSystem{

    private String fileName="default";
    public EFileSystem() {
    }

    public EFileSystem(String fileName) {
        this.fileName = fileName;
    }

    public EFileSystem(String fileName,String fileSystem){
        super(fileSystem);
        this.fileName = fileName;

    }
    public FSInputStream open(String path){
        ClusterInfo clusterInfo = getClusterInfo();
        ClusterInfo.MetaServerMsg masterMetaServer = clusterInfo.getMasterMetaServer();
        if (masterMetaServer==null){
            throw new RuntimeException("metaserver服务器不可用！");
        }
        String targetUrl = masterMetaServer.getHost()+":"+masterMetaServer.getPort() + "/open";
        ConcurrentHashMap<String,String> param = new ConcurrentHashMap<>();
        param.put("path", path);
        CloseableHttpResponse response = callRemote(targetUrl, param, HttpMethod.GET);
        System.out.println("请求open获得状态码："+response.getCode());
        FSInputStream fsInputStream = new FSInputStream(path, this);
        return fsInputStream;
    }
    public FSOutputStream create(String path){
        ClusterInfo clusterInfo = getClusterInfo();
        ClusterInfo.MetaServerMsg masterMetaServer = clusterInfo.getMasterMetaServer();
        if (masterMetaServer==null){
            throw new RuntimeException("metaserver服务器不可用！");
        }
        String targetUrl = masterMetaServer.getHost()+":"+masterMetaServer.getPort() + "/create";
        ConcurrentHashMap<String,String> param = new ConcurrentHashMap<>();
        param.put("path", path);
        CloseableHttpResponse response = callRemote(targetUrl,param, HttpMethod.POST);
        System.out.println("请求create获得状态码："+response.getCode());

        FSOutputStream fsOutputStream = new FSOutputStream(path,this);
        return fsOutputStream;
    }
    public boolean mkdir(String path){
        ClusterInfo clusterInfo = getClusterInfo();
        ClusterInfo.MetaServerMsg masterMetaServer = clusterInfo.getMasterMetaServer();
        if (masterMetaServer==null){
            throw new RuntimeException("metaserver服务器不可用！");
        }
        String targetUrl = masterMetaServer.getHost()+":"+masterMetaServer.getPort() + "/mkdir";
        ConcurrentHashMap<String,String> param = new ConcurrentHashMap<>();
        param.put("path", path);
        CloseableHttpResponse response = callRemote(targetUrl, param, HttpMethod.POST);
        return handleResponse(response,Boolean.class);
    }
    public boolean delete(String path){
        ClusterInfo clusterInfo = getClusterInfo();
        ClusterInfo.MetaServerMsg masterMetaServer = clusterInfo.getMasterMetaServer();
        if (masterMetaServer==null){
            throw new RuntimeException("metaserver服务器不可用！");
        }
        String targetUrl = masterMetaServer.getHost()+":"+masterMetaServer.getPort() + "/delete";
        ConcurrentHashMap<String,String> param = new ConcurrentHashMap<>();
        param.put("path", path);
        CloseableHttpResponse response = callRemote(targetUrl, param, HttpMethod.GET);
        return handleResponse(response,Boolean.class);
    }

    /**
     * 获取文件元数据
     * @param path
     * @return
     */
    public StatInfo getFileStats(String path){
        ClusterInfo clusterInfo = getClusterInfo();
        ClusterInfo.MetaServerMsg masterMetaServer = clusterInfo.getMasterMetaServer();
        if (masterMetaServer==null){
            throw new RuntimeException("metaserver服务器不可用！");
        }
        String targetUrl = masterMetaServer.getHost()+":"+masterMetaServer.getPort() + "/stats";
        ConcurrentHashMap<String,String> param = new ConcurrentHashMap<>();
        param.put("path", path);
        CloseableHttpResponse response = callRemote(targetUrl, param, HttpMethod.GET);
        return handleResponse(response,StatInfo.class);
    }

    /**
     * 只列出path这一层的文件或文件夹, 不包含子目录下的文件或文件夹
     * @param path
     * @return
     */
    public List<StatInfo> listFileStats(String path){
        ClusterInfo clusterInfo = getClusterInfo();
        ClusterInfo.MetaServerMsg masterMetaServer = clusterInfo.getMasterMetaServer();
        if (masterMetaServer==null){
            throw new RuntimeException("metaserver服务器不可用！");
        }
        String targetUrl = masterMetaServer.getHost()+":"+masterMetaServer.getPort() + "/listdir";
        ConcurrentHashMap<String,String> param = new ConcurrentHashMap<>();
        param.put("path", path);
        CloseableHttpResponse response = callRemote(targetUrl, param, HttpMethod.GET);
        return (List<StatInfo>) handleResponse(response,List.class);
    }

    /**
     * 获取集群
     * @return
     */
    public ClusterInfo getClusterInfo(){
        ZkUtil zkUtil = new ZkUtil();
        return zkUtil.getClusterInfo();
    }

}
